#include "cngameplan.h"

cnGamePlan::cnGamePlan(QObject *parent)
{
    //this->setParent(parent);
}

int cnGamePlan::generate(int size, int w, int h, char *pattern)
{
    int i;
    plan.resize(size);
    int horizontal_size = size / 4;
    if (size % 4) horizontal_size++;
    int vertical_size = size / 4;
    int cell_size = (w <= h ? w : h) / ((w <= h ? horizontal_size : vertical_size) + 5);
    int x = (w / 2) - ((cell_size * (horizontal_size + 5)) / 2) + 2 * cell_size;
    int y = (h / 2) - ((cell_size * (vertical_size + 5)) / 2) + 2 * cell_size;

    for (i = 0; i < size; i++)
    {
        plan[i] = new QLabel(this);
        switch (pattern[i])
        {
            case 1:
                plan[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_red.png")));
                break;
            case 2:
                plan[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_green.png")));
                break;
            case 3:
                plan[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_blue.png")));
                break;
            case 4:
                plan[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_grey.png")));
                break;
            case 5:
                plan[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_yelow.png")));
                break;
            case 6:
                plan[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_purple.png")));
                break;
        }
        plan[i]->setScaledContents(true);
        if (i < horizontal_size)
        {
            if (i == (horizontal_size / 2 + 1))
                plan[i]->setGeometry(x - 4,y - 4,cell_size + 8,cell_size + 8);
            else
                plan[i]->setGeometry(x,y,cell_size,cell_size);
            x += cell_size;
        }
        else
        if (i < (vertical_size + horizontal_size))
        {
            if (i == (vertical_size / 2 + 1 + horizontal_size))
                plan[i]->setGeometry(x - 4,y - 4,cell_size + 8,cell_size + 8);
            else
                plan[i]->setGeometry(x,y,cell_size,cell_size);
            y += cell_size;
        }
        else
        if (i < (vertical_size + (horizontal_size * 2)))
        {
            if (i == (horizontal_size + vertical_size + horizontal_size / 2 + 1))
                plan[i]->setGeometry(x - 4,y - 4,cell_size + 8,cell_size + 8);
            else
                plan[i]->setGeometry(x,y,cell_size,cell_size);
            x -= cell_size;
        }
        else
        if (i < ((vertical_size * 2) + (horizontal_size * 2)))
        {
            if (i == (horizontal_size + vertical_size + horizontal_size + vertical_size / 2 + 1))
                plan[i]->setGeometry(x - 4,y - 4,cell_size + 8,cell_size + 8);
            else
                plan[i]->setGeometry(x,y,cell_size,cell_size);
            y -= cell_size;
        }
    }

    home.resize(16);
    end.resize(16);

    for (i = 0; i < 16; i++)
    {
        home[i] = new QLabel(this);
        end[i] = new QLabel(this);
        home[i]->setScaledContents(true);
        end[i]->setScaledContents(true);
        if (i < 4)
        {
             home[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_blue.png")));
             end[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_blue.png")));
        }
        else
        if (i < 8)
        {
             home[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_red.png")));
             end[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_red.png")));
        }
        else
        if (i < 12)
        {
             home[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_green.png")));
             end[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_green.png")));
        }
        else
        {
             home[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_purple.png")));
             end[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_purple.png")));
        }
    }

    for (i = 0; i < 16; i++)
    {
        switch (pattern[size + i])
        {
            case 1:
                end[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_red.png")));
                break;
            case 2:
                end[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_green.png")));
                break;
            case 3:
                end[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_blue.png")));
                break;
            case 4:
                end[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_grey.png")));
                break;
            case 5:
                end[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_yelow.png")));
                break;
            case 6:
                end[i]->setPixmap(QPixmap(QString::fromUtf8("resources/policko_purple.png")));
                break;
        }
    }

    home[4]->setGeometry(w / 2 - cell_size,0,cell_size,cell_size);
    home[5]->setGeometry(w / 2,0,cell_size,cell_size);
    home[6]->setGeometry(w / 2 - cell_size,cell_size,cell_size,cell_size);
    home[7]->setGeometry(w / 2,cell_size,cell_size,cell_size);

    home[0]->setGeometry(0,h / 2 - cell_size,cell_size,cell_size);
    home[1]->setGeometry(cell_size,h / 2 - cell_size,cell_size,cell_size);
    home[2]->setGeometry(0,h / 2,cell_size,cell_size);
    home[3]->setGeometry(cell_size,h / 2,cell_size,cell_size);

    home[8]->setGeometry(w - 2 * cell_size,h / 2 - cell_size,cell_size,cell_size);
    home[9]->setGeometry(w - cell_size,h / 2 - cell_size,cell_size,cell_size);
    home[10]->setGeometry(w - 2 * cell_size,h / 2,cell_size,cell_size);
    home[11]->setGeometry(w - cell_size,h / 2,cell_size,cell_size);

    home[12]->setGeometry(w / 2 - cell_size,h - 2 * cell_size,cell_size,cell_size);
    home[13]->setGeometry(w / 2,h - 2 * cell_size,cell_size,cell_size);
    home[14]->setGeometry(w / 2 - cell_size,h - cell_size,cell_size,cell_size);
    home[15]->setGeometry(w / 2,h - cell_size,cell_size,cell_size);

    x = (w / 2) - ((cell_size * (horizontal_size + 5)) / 2) + 2 * cell_size;
    y = (h / 2) - ((cell_size * (vertical_size + 5)) / 2) + 2 * cell_size;

    end[0]->setGeometry(x + cell_size,y + (vertical_size / 2 + vertical_size % 2) * cell_size,cell_size,cell_size);
    end[1]->setGeometry(x + cell_size * 2,y + (vertical_size / 2 + vertical_size % 2) * cell_size,cell_size,cell_size);
    end[2]->setGeometry(x + cell_size * 3,y + (vertical_size / 2 + vertical_size % 2) * cell_size,cell_size,cell_size);
    end[3]->setGeometry(x + cell_size * 4,y + (vertical_size / 2 + vertical_size % 2) * cell_size,cell_size,cell_size);

    end[4]->setGeometry(x + (horizontal_size / 2) * cell_size,y + cell_size,cell_size,cell_size);
    end[5]->setGeometry(x + (horizontal_size / 2) * cell_size,y + cell_size * 2,cell_size,cell_size);
    end[6]->setGeometry(x + (horizontal_size / 2) * cell_size,y + cell_size * 3,cell_size,cell_size);
    end[7]->setGeometry(x + (horizontal_size / 2) * cell_size,y + cell_size * 4,cell_size,cell_size);

    end[8]->setGeometry(x + (horizontal_size - 1) * cell_size,y + (vertical_size / 2) * cell_size,cell_size,cell_size);
    end[9]->setGeometry(x + (horizontal_size - 2) * cell_size,y + (vertical_size / 2) * cell_size,cell_size,cell_size);
    end[10]->setGeometry(x + (horizontal_size - 3) * cell_size,y + (vertical_size / 2) * cell_size,cell_size,cell_size);
    end[11]->setGeometry(x + (horizontal_size - 4) * cell_size,y + (vertical_size / 2) * cell_size,cell_size,cell_size);

    end[12]->setGeometry(x + (horizontal_size / 2 + horizontal_size % 2) * cell_size,y + (vertical_size - 1) * cell_size,cell_size,cell_size);
    end[13]->setGeometry(x + (horizontal_size / 2 + horizontal_size % 2) * cell_size,y + (vertical_size - 2) * cell_size,cell_size,cell_size);
    end[14]->setGeometry(x + (horizontal_size / 2 + horizontal_size % 2) * cell_size,y + (vertical_size - 3) * cell_size,cell_size,cell_size);
    end[15]->setGeometry(x + (horizontal_size / 2 + horizontal_size % 2) * cell_size,y + (vertical_size - 4) * cell_size,cell_size,cell_size);

    return 0;
}

int cnGamePlan::setPlayers(int count, int player)
{
    int i,j;
    int max = count;
    QString path;
    local = player + 1;
    players.resize(16);
    for (i = 1; i < max; i++)
    {
        path = "";
        path.append("resources/f");
        path.append(QString::number(i));
        path.append(".png");
        if (i == (player + 1))
            max++;
        else
            for (j = 0; j < 4; j++)
            {
                players[(i - 1) * 4 + j] = new cnFigurine(this);
                players[(i - 1) * 4 + j]->setPixmap(QPixmap(QString::fromUtf8(path.toUtf8())));
                players[(i - 1) * 4 + j]->setScaledContents(true);
                players[(i - 1) * 4 + j]->setGeometry(home[(i - 1) * 4 + j]->geometry());
                QObject::connect(players[(i - 1) * 4 + j],SIGNAL(clicked(cnFigurine*)),this,SLOT(selected(cnFigurine*)));
            }
    }
    path = "";
    path.append("resources/f");
    path.append(QString::number(player + 1));
    path.append(".png");
    for (j = 0; j < 4; j++)
    {
        players[player * 4 + j] = new cnFigurine(this);
        players[player * 4 + j]->setPixmap(QPixmap(QString::fromUtf8(path.toUtf8())));
        players[player * 4 + j]->setScaledContents(true);
        players[player * 4 + j]->setGeometry(home[player * 4 + j]->geometry());
        QObject::connect(players[player * 4 + j],SIGNAL(clicked(cnFigurine*)),this,SLOT(selected(cnFigurine*)));
    }
    return 0;
}

void cnGamePlan::selected(cnFigurine *src)
{

}
